//
//  AnalyticsManager.m
//  suRead
//
//  Created by colin on 2019/2/28.
//  Copyright © 2019 bao. All rights reserved.
//

#import "AnalyticsManager.h"
#import "AccountManager.h"
#import <SHWAnalyticsSDK/SHWAnalyticsSDK.h>

@implementation AnalyticsManager
+ (void)event:(AnalyticsEventType)type targetId:(NSString *)targetId url:(NSString *)url {
    
    if (targetId && [targetId length] > 0) {
        AccountManager *manager = [AccountManager sharedAccountManager];
        NSMutableDictionary *args = [NSMutableDictionary dictionary];
        
        //稿件id 该字段为必需
        [args setValue:targetId forKey:@"targetID"];
        
        //IP地址 该字段为必需
        [args setValue:manager.f_ip forKey:@"ip"];

        //媒体机构 该字段为必需
        [args setValue:AnalyticsOrgId forKey:@"organization"];
        
        //APP对应的APP标识（即APPKey) 该字段为必需
        [args setValue:AnalyticsAppKey forKey:@"applicationID"];
        
        [args setValue:url forKey:@"url"];
        
        if (manager.f_isLogined) {
            // 如用户已登录，该字段为必需
            [args setValue:manager.f_uid forKey:@"userid"];
            
            // 用户性别
            // [args setValue:@"" forKey:@"sex"];
            
            //用户职业
            // [args setValue:@"" forKey:@"profession"];
            
            //用户年龄
            // [args setValue:@"" forKey:@"age"];
        }
        
        NSString *eventType = @"";
        switch (type) {
            case AnalyticsEventType_comeIn:
                eventType = @"comeIn";
                break;
            case AnalyticsEventType_leave:
                eventType = @"leave";
                break;
            case AnalyticsEventType_praise:
                eventType = @"praise";
                break;
            case AnalyticsEventType_forward:
                eventType = @"forward";
                break;
            case AnalyticsEventType_comment:
                eventType = @"comment";
                break;
            case AnalyticsEventType_playVideo:
                eventType = @"playVideo";
                break;
        }
        
        if ([eventType length] > 0) {
            
            NSLog(@"type---%@   args---%@",eventType,args);
            
            // 普通埋点，按照配置的上报规则进行上报
            [SHWAnalyticsSDKInterface customEvent:eventType args:args];
            // 立即上报埋点（流量消耗较多）
            // [SHWAnalyticsSDKInterface recordImmediatelyUploadEvent:eventType args:args];
        } else {
            NSLog(@"eventType不匹配");
        }
        
    } else {
        NSLog(@"targetId为空");
    }
    
    /*
    // 以下未标识必需的字段，在可以获取的情况下尽量获取，如果确实不能获取，可以传递为空
    NSDictionary *dict = @{@"userid" : @"此处填写已登录用户ID",  // 如用户已登录，该字段为必需
                           @"sex" : @"此处填写已登录用户性别",
                           @"profession" : @"此处填写已登录用户职业",
                           @"age" : @"此处填写已登录用户年龄",
                           @"ip" : @"此处填写IP地址", // 该字段为必需
                           @"targetID" : @"此处填写稿件ID", // 该字段为必需
                           @"url" : @"此处填写url",
                           @"organization" : @"此处填写媒体机构ID", // 该字段为必需
                           @"applicationID" : @"此处填写APP对应的APP标识（即APPKey)" // 该字段为必需
                           };
     */
    
}

+ (void)eventComment:(NSString *)targetId url:(NSString *)url targetUrl:(NSString *)targetUrl text:(NSString *)text {
    
    if (targetId && [targetId length] > 0) {
        AccountManager *manager = [AccountManager sharedAccountManager];
        NSMutableDictionary *args = [NSMutableDictionary dictionary];
        
        //稿件id 该字段为必需
        [args setValue:targetId forKey:@"targetID"];
        
        //IP地址 该字段为必需
        [args setValue:manager.f_ip forKey:@"ip"];
        
        //媒体机构 该字段为必需
        [args setValue:AnalyticsOrgId forKey:@"organization"];
        
        //APP对应的APP标识（即APPKey) 该字段为必需
        [args setValue:AnalyticsAppKey forKey:@"applicationID"];
        
        [args setValue:url forKey:@"url"];
        
        [args setValue:targetUrl forKey:@"targetURL"];
        
        [args setValue:text forKey:@"comment"];

        if (manager.f_isLogined) {
            // 如用户已登录，该字段为必需
            [args setValue:manager.f_uid forKey:@"userid"];
            
            // 用户性别
            // [args setValue:@"" forKey:@"sex"];
            
            //用户职业
            // [args setValue:@"" forKey:@"profession"];
            
            //用户年龄
            // [args setValue:@"" forKey:@"age"];
        }
        
        // 普通埋点，按照配置的上报规则进行上报
        [SHWAnalyticsSDKInterface customEvent:@"comment" args:args];
        // 立即上报埋点（流量消耗较多）
        // [SHWAnalyticsSDKInterface recordImmediatelyUploadEvent:eventType args:args];
    } else {
        NSLog(@"targetId为空");
    }
    
    /*
     // 以下未标识必需的字段，在可以获取的情况下尽量获取，如果确实不能获取，可以传递为空
     NSDictionary *dict = @{@"userid" : @"此处填写已登录用户ID",  // 如用户已登录，该字段为必需
     @"sex" : @"此处填写已登录用户性别",
     @"profession" : @"此处填写已登录用户职业",
     @"age" : @"此处填写已登录用户年龄",
     @"ip" : @"此处填写IP地址", // 该字段为必需
     @"targetID" : @"此处填写稿件ID", // 该字段为必需
     @"url" : @"此处填写url",
     @"organization" : @"此处填写媒体机构ID", // 该字段为必需
     @"applicationID" : @"此处填写APP对应的APP标识（即APPKey)" // 该字段为必需
     };
     */
    
}

@end
